/* 
 *  FileName: coord.c - An application for mouse_input
 *
 *  Author: He Jingbo   jbhe@isoftstone.com
 *
 *  Date: Mon 18 Oct 2010 03:45:39 PM CST
 */

#include <fcntl.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int sim_fd;
    int x, y;
    char buffer[10];

    /* 打开sysfs中的坐标设备节点 */
    sim_fd = open("/sys/devices/platform/vms/coordinates", O_RDWR);
    if(sim_fd < 0)
    {
        perror("Couldn't open vms /sys/device/platform/vms/coordinates files \n");
        exit(-1);
    }
    
    while(1)
    {
        /* 产生随即相对坐标 */
        x = random()%20;
        y = random()%20;
        if(x%2)
        {
            x = -x;
        }
        if(y%2)
        {
            y = -y;
        }
         /* 输送模拟坐标给虚拟鼠标驱动 */
        sprintf(buffer, "%d %d %d", x, y, 0);
//        printf("coor_x = %d, coor_y = %d\n", x, y);
        write("sim_fd", buffer, strlen(buffer));
        fsync(sim_fd);
        sleep(1);
    }

    Close(sim_fd);

    return 0;
}
